package dog.gui.appointmentView;

import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;

import javax.swing.*;

import constants.STATUS;
import containers.Appointment;
import containers.Person;
import dog.gui.ParticipantCell;
import dog.gui.ParticipantCellRenderer;
import dog.gui.PersonCellRenderer;
import dog.gui.PersonList;
import dog.model.Model;

@SuppressWarnings("serial")
public class InvitesListPanel extends JPanel implements ActionListener{
	
	private DefaultListModel listModelR;
	private PersonList listL;
	private JList listR;
	private JButton inviteButton;
	private JButton unInviteButton;
	private Model model;

	public InvitesListPanel(AppointmentPanel appointmentPanel, Model model){
		super();
		this.model=model;
		listModelR = new DefaultListModel();
		
		GridBagLayout layout = new GridBagLayout();
		setLayout(layout);
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(2, 10, 2, 1);
		c.fill = GridBagConstraints.VERTICAL;
		
		listL = new PersonList(model,null);
		JScrollPane scrollPane;
		scrollPane = new JScrollPane(listL);
		c.gridx = 0;
		c.gridy = 0;
		c.gridheight=2;
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		add(scrollPane,c);
		
		c.fill=GridBagConstraints.HORIZONTAL;
		c.anchor=GridBagConstraints.NORTH;
		c.gridheight=1;
		unInviteButton = new JButton();
		c.gridx = 1;
		c.gridy = 0;
		unInviteButton.setText("Remove");
		unInviteButton.addActionListener(this);
		add(unInviteButton,c);
		
		inviteButton = new JButton();
		c.gridx = 1;
		c.gridy = 1;
		inviteButton.setText("Invite");
		inviteButton.addActionListener(this);
		add(inviteButton,c);
		
		c.fill = GridBagConstraints.VERTICAL;
		c.gridheight=2;
		listR = new JList(listModelR);
		listR.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); 
		listR.setCellRenderer(new ParticipantCellRenderer());
		listR.setPrototypeCellValue(new ParticipantCell(null,-1,-1));
		scrollPane = new JScrollPane(listR);
		c.gridx = 2;
		c.gridy = 0;
		add(scrollPane,c);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) 
		{
		if (arg0.getSource().equals(inviteButton)) 
			{
			int[] toAdd = listL.getSelectedIDs();
			for(int i=0; i<toAdd.length; i++)
				{
				ParticipantCell pc = new ParticipantCell(model,toAdd[i],STATUS.NOTREPLIED);
				if (!listModelR.contains(pc) && toAdd[i]!=model.getPersonID())
					{
					listModelR.addElement(pc);
					}
				if(toAdd[i]==model.getPersonID())
					JOptionPane.showMessageDialog(null, "You can't invite yourself to your own appointment", "Error", JOptionPane.ERROR_MESSAGE);
				}
			}
		else if (arg0.getSource().equals(unInviteButton)) 
			{
			ArrayList<ParticipantCell> ids = new ArrayList<ParticipantCell>();
			for(int i=0; i<listModelR.size(); i++)
				{
				if (listR.isSelectedIndex(i))
					{
					ids.add((ParticipantCell)listModelR.elementAt(i));
					}
				}
			for(int i=0; i<ids.size(); i++)
				{
				listModelR.removeElement(ids.get(i));
				}
			}
		}

	public void setEditable(boolean b)
		{
		inviteButton.setEnabled(b);
		unInviteButton.setEnabled(b);
		}

	public void updateToAppointment(Appointment oldAppointment, Appointment newAppointment)
		{
		//Extract a list of participants and a list of statuses
		listModelR.removeAllElements();
		if(oldAppointment!=null)
			{
			HashMap<Integer,Integer> statusMap = oldAppointment.getStatuses();
			Iterator<Entry<Integer,Integer>> it = statusMap.entrySet().iterator();
			while (it.hasNext())
				{
				Entry<Integer,Integer> entry = it.next();
				listModelR.addElement(new ParticipantCell(model,entry.getKey(),entry.getValue()));
				}
			}
		}

	public ArrayList<Integer> getParticipants()
		{
		ArrayList<Integer> result = new ArrayList<Integer>();
		for(int i=0; i<listModelR.size(); i++)
			{
			ParticipantCell pc = (ParticipantCell)listModelR.get(i);
			System.out.println("Inviteslist has person "+pc.getPersonID());
			result.add(pc.getPersonID());
			}
		return result;
		}
	public void setDefaultStatuses()
		{
		for(int i=0; i<listModelR.size(); i++)
			{
			ParticipantCell pc = (ParticipantCell)listModelR.get(i);
			if(pc.getPersonID()==model.getPersonID())
				pc.setLocalStatus(STATUS.OWNER);
			else
				pc.setLocalStatus(STATUS.NOTREPLIED);
			}
		listR.setCellRenderer(new ParticipantCellRenderer());
		}
}
